<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>继承的学习</title>
</head>
<body>
	<script type="text/javascript">
	//组合继承，借用构造函数来继承属性，原型来继承方法
		function Person (name,age) {
			this.name    = name;
			this.age     = age;
			this.friends = ['Jay','Hanna','Vicky'];
			console.log(this);
		}

		var p1 = Person("xiaoming",3);
		var p2 = new Person("xiaohong",3);
		// p1 this指向为window，p2this指向为Person，p1是调用Person方法，将其属性和方法都加在了window对象上。
		Person.prototype.sayHello = function () {
			console.log(this.name + 'Hello!');
		}
		function Student (name,age,sex) {
			Person.call(this,name,age);
			this.sex = sex;
		}

		Student.prototype = new Person();
		Student.prototype.constructor = Student;
		Student.prototype.isGirl = function () {
			var isGirl;
			if(this.sex !== "female"){
				isGirl = false;
			} else {
				isGirl = true;
			}
			console.log("isGirl:" + isGirl);
		};
		var student1 = new Student ("xiaoming",22,"male");
		student1.friends.push("David");
		console.log(student1.friends + "    " );
		student1.sayHello();
		alert(student1.constructor);
		var student2 = new Student("xiaohong",23,"female");
		console.log(student2.friends);
		student2.isGirl();
		//组合继承的缺点：会两次调用父类的构造函数，一次是创建子类原型的时候，一次是调用子类子类构造函数的时候。改进方法：使用寄生组合式继承，不必为了指定子类型的原型而调用父类的构造函数，我们所需要的无非是超类型原型的一个副本而已。
		function object(o){
			function F () {}
			F.prototype = o;
			return new F();
		}
		function inheritPrototype (child,parent) {
			var prototype         = Object.create(parent.prototype); 
			prototype.constructor = child;
			child.prototype       = prototype;
		}
		function extend (Child,Parent) {
			function F () {}
			F.prototype = Parent.prototype;
			Child.prototype = new F();
			Child.prototype.constructor = Child;
			Child.uber = Parent.prototype;
		}


		function Singer (name,age,country) {
			Person.call(this,name,age);
			this.country = country;
		}
		//inheritPrototype(Singer,Person);
		extend(Singer,Person);
		Singer.prototype.fromWhere = function () {
			console.log("The singer is from:" + this.country);
		}

		var singer1 = new Singer("Jay",37,"China");
		singer1.sayHello();
		singer1.fromWhere();



//测试 为什么不让子类的原型=父类的原型
		function Father() {}
		Father.prototype.type = "Person";

		function Son(name) {
			this.name = name;
		}
		console.log(Father.prototype.constructor);
		Son.prototype = Father.prototype;
		Son.prototype.constructor = Son;
		var son = new Son("Liming");
		console.log(son.type);
		console.log(Father.prototype.constructor);




	</script>
</body>
</html>